var plugin_shareprojects = {
	_showTopbar:function() {
		if($defined(user.addProject)) {
			var details = user.addProject;
			var thisplugin = this;
			floater = new Floating({
				width:300,
				windowtitle:'Share Project',
				content:'Do your want to gain access to '+details.u_name+'\'s project "'+details.p_name+'"?<br><br><div class="button" id="shareprojects_accept" >accept</div> <div class="button"  id="shareprojects_decline" >decline</div>'
			});
			
			$('shareprojects_accept').addEvent('click', this.accept);
			$('shareprojects_decline').addEvent('click', this.decline);
		}
	},
	
	event:function(type,data) {
		if(type=="joel.drawTasklist") this._drawTasklist();
		if(type=='summary.show') this.summary_show();
	},
	
	_drawTasklist:function() {
		var parent = this;
		var link = new Element('div',{
			id:"shareproject_link",
			'html':'share this project',
		    'events': {
		        'click': function () { 
		        	parent.floater = new Modal({
						width:300,
						windowtitle:'Share Project',
						content:'Please enter the email address of the person to invite to this project:<br><br><input type="text" id="shareproject_with"><br> <div class="button" id="shareprojects_send" >send invitation</div><br><br><div id="send_result" style="clear:both"></div>'
					});
					
					$('shareprojects_send').addEvent('click', function () {
						$('send_result').innerHTML = 'sending request...';
						if(utils.isEmail($('shareproject_with').value)) {
							request('shareprojects:sendInvitation',{email:$('shareproject_with').value,project_id:project},function (res) {
								if(res) {
									$('send_result').innerHTML = res;
								} else {
									$('send_result').innerHTML = 'Invitation sent.';
									setTimeout(function() { if($defined(floater)) { floater.close(); }},3000);
								}
							});
						} else {
							$('send_result').innerHTML = 'please enter a valid email address';
						}
						
					});
		        }
		     }
		});
		
		link.inject($('tasklist'), 'top');
	},
	
	summary_show:function() {
		var plugin = this;
		projects.each(function(e) {
			if($defined(e.shared)) { var thisproject = e.project_id; var trashcan = $('sum_'+thisproject).getElement(".delproject").onclick = function() { plugin.delProject(thisproject); }}
		});
	},
	
	delProject:function(project_id) {
		var floating = new Modal({
			windowtitle:'Delete Project',
			content:'This is a shared project. Do you want to remove it from your projects list?<br><br><br><div class="button" id="delProject_yes"> yes </div> <div class="button" id="delProject_no"> cancel </div><br><br>'
		});
		
		$('delProject_yes').onclick=function() {
			request('shareprojects:delProject',{'project_id':project_id},function(res) {
				if(res) { alert(res); } else {
					floating.close();
					projects.each(function(e,i) { if(e.project_id == project_id) { projects.splice(i,1); }});
					BC.broadcastMessage('_project_delete', project_id);
				}
			});
		}
		
		$('delProject_no').onclick=function() {
			floating.close();
		}
	},
	
	accept:function() {
		request('shareprojects:accept',{},function(res) { if(res) { alert(res); } else { 
			floater.close();
			request('getProjects','',function (response) {
				if(response != 'logout') {
					projects = JSON.decode(response);
					selectProject(user.addProject.project_id);
					delete user.addProject;
				} else {
					logout();
				}
			});
		}});
	},
	
	decline:function() {
		request('shareprojects:decline',{},function(res) { if(res) { alert(res); } else { floater.close(); }});
		delete user.addProject;
	}
}